package com.mljr.example.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.ctakit.sdk.app.BaseApplication;
import com.mcxiaoke.packer.helper.PackerNg;
import com.mljr.example.BuildConfig;
import com.mljr.example.ui.h5.Html5Activity;
import com.mljr.example.ui.home.HomeActivity;
import com.orhanobut.logger.Logger;

import java.util.LinkedList;
import java.util.List;

/**
 * android退出程序的工具类，使用单例模式
 * 1.在Activity的onCreate()的方法中调用addActivity()方法添加到mActivityList
 * 2.你可以在Activity的onDestroy()的方法中调用delActivity()来删除已经销毁的Activity实例
 * 这样避免了mActivityList容器中有多余的实例而影响程序退出速度
 *
 * @author andyhome
 */
public class AppUtils {
    private static BaseApplication mContext;
    /**
     * 转载Activity的容器
     */
    private static List<Activity> mActivityList = new LinkedList<Activity>();
    private static AppUtils instance = new AppUtils();

    /**
     * 将构造函数私有化
     */
    private AppUtils() {
    }

    ;

    /**
     * 获取ExitAppUtils的实例，保证只有一个ExitAppUtils实例存在
     *
     * @return
     */
    public static AppUtils getInstance() {
        return instance;
    }

    public int getActivityCount() {
        return mActivityList.size();
    }

    /**
     * 添加Activity实例到mActivityList中，在onCreate()中调用
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        mActivityList.add(activity);
        if (activity != null && activity.getApplicationContext() != null) {
            mContext = (BaseApplication) activity.getApplicationContext();
        }

    }

    public static void setInstance(BaseApplication context) {
        mContext = context;
    }

    /**
     * 从容器中删除多余的Activity实例，在onDestroy()中调用
     *
     * @param activity
     */
    public void delActivity(Activity activity) {
        mActivityList.remove(activity);
    }

    public static BaseApplication getContext() {
        return mContext;
    }



    /**
     * 退出程序的方法
     */
    public void exit() {
        for (Activity activity : mActivityList) {
            activity.finish();
        }
    }

    public static  void exitSys() {
        for (Activity activity : mActivityList) {
            activity.finish();
        }
        System.exit(0);
    }

    public void removeAll() {
        for (Activity activity : mActivityList) {
            if (activity instanceof HomeActivity || activity instanceof Html5Activity) {
                continue;
            }
            activity.finish();
        }

    }


    public static String getChannelCode() {

// com.mcxiaoke.packer.helper.PackerNg
        final String market = PackerNg.getMarket(getContext(), "mljr");
// 或者使用 PackerNg.getMarket(Context,defaultValue)
        Logger.d("market:"+ market);
        return market;

    }

    public static String getSystemInfoUrl(String url) {

        url = (url.indexOf("?")) < 0 ? (url + "?appChannel=" + AppUtils.getChannelCode()) : url + "&appChannel=" + AppUtils.getChannelCode();


        return url;

    }

    public static boolean isEmulator(Context context) {
        if (BuildConfig.DEBUG) {
            return false;
        }

        String imei = getImei();
        String serial = android.os.Build.SERIAL;
        String model = Build.MODEL;
        String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        ;

        Logger.d("emulator_info-imei:" + imei);
        Logger.d("emulator_info-serial:" + serial);
        Logger.d("emulator_info-model:" + model);
        Logger.d("emulator_info-PRODUCT:" + Build.PRODUCT);
        if ("000000000000000".equals(imei) || TextUtils.isEmpty(android_id) || CheckEmulatorBuild()) {
            return true;
        }
        return false;
    }

    public static String getImei() {
        String imei = "";
        try {
            TelephonyManager tm = (TelephonyManager) AppUtils.getContext().getSystemService(Context.TELEPHONY_SERVICE);
            imei = tm.getDeviceId();
        } catch (Exception e) {

        }

        return imei;
    }

    public static Boolean CheckEmulatorBuild() {
        String BOARD = android.os.Build.BOARD;
        String BOOTLOADER = android.os.Build.BOOTLOADER;
        String BRAND = android.os.Build.BRAND;
        String DEVICE = android.os.Build.DEVICE;
        String HARDWARE = android.os.Build.HARDWARE;
        String MODEL = android.os.Build.MODEL;
        String PRODUCT = android.os.Build.PRODUCT;
        if ("generic".equalsIgnoreCase(BRAND) || "generic".equalsIgnoreCase(DEVICE) || "sdk".equalsIgnoreCase(MODEL) || "sdk".equalsIgnoreCase(PRODUCT) || "goldfish".equalsIgnoreCase(HARDWARE)) {
            return true;
        }
        return false;
    }

    private static String getMetaData(Context context, String key) {

        try {

            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(

                    context.getPackageName(), PackageManager.GET_META_DATA);

            Object value = ai.metaData.get(key);

            if (value != null) {

                return value.toString();

            }

        } catch (Exception e) {

            //

        }

        return null;

    }

}